<template>
  <div class="custom_listpinterest">
    <h1 class="custom_listpinterest_header" v-if="item.title">
      <div class="custom_listpinterest_header_title">{{ item.title }}</div>
      <div class="custom_listpinterest_header_slogan">{{ item.slogan }}</div>
      <div><slot name="right"></slot></div>
    </h1>
    <div class="custom_listpinterest_content" v-if="listData.length > 0">
      <div
        class="custom_listpinterest_item left"
        v-if="item.listRowsCount === 1 || item.listRowsCount >= 2"
        :class="item.listRowsCount === 1 && 'single'"
      >
        <lazy-component>
          <ListPinterestItem
            :item="dataLeftListData"
            :actions="actions"
            :listRowsCount="item.listRowsCount"
          ></ListPinterestItem>
        </lazy-component>
      </div>
      <div
        class="custom_listpinterest_item center"
        v-if="item.listRowsCount > 2"
      >
        <lazy-component>
          <ListPinterestItem
            :item="dataCenterListData"
            :actions="actions"
            :listRowsCount="item.listRowsCount"
          ></ListPinterestItem>
        </lazy-component>
      </div>
      <div
        class="custom_listpinterest_item right"
        v-if="item.listRowsCount >= 2"
      >
        <lazy-component>
          <ListPinterestItem
            :item="dataRightListData"
            :actions="actions"
            :listRowsCount="item.listRowsCount"
          ></ListPinterestItem>
        </lazy-component>
      </div>
    </div>
    <div v-else class="custom_listpinterest_content nodata">
      <span>{{ $t("lang.暂无数据") }}</span>
    </div>
    <div
      class="loadmore"
      v-if="!isDownLoadMore && !isLoading && listData.length > 0"
    >
      <a href="javascript:;" @click="loadMoreClickHandle">{{
        $t("lang.点击加载更多")
      }}</a>
    </div>
    <div class="loading" v-if="isLoading">
      <van-loading size="24px">{{ $t("lang.加载中") }}</van-loading>
    </div>
  </div>
</template>
<script>
export default {
  name: "listPinterest",
  components: {
    ListPinterestItem: () =>
      import(
        /* webpackChunkName:'listPinterestItem' */ "../../components/listPinterest/listPinterestItem"
      ),
  },
  props: {
    item: {
      type: Object,
      default() {
        return {};
      },
    },
    index: {
      type: Number,
      default() {
        return 0;
      },
    },
    actions: {
      type: Array,
      default() {
        return [];
      },
    },
    active: {
      type: Number,
      default() {
        return 0;
      },
    },
    isDownLoadMore: {
      type: Boolean,
      default() {
        return true;
      },
    },
    maxLength: {
      type: Number,
      default() {
        return 20;
      },
    },
  },
  data() {
    return {
      dataLeftListData: [],
      dataCenterListData: [],
      dataRightListData: [],
      listData: [],
      isLoading: false,
      listRowsCount: 2,
    };
  },
  beforeDestroy() {
    let that = this;
    that.listPinterestTimeout && clearTimeout(that.listPinterestTimeout);
    that.$query(window).off("scroll");
  },
  created() {
    this.requestData(this.$props.item);
  },
  mounted() {
    this.$on("refresh", this.requestData);
    let that = this;
    if (this.$props.isDownLoadMore) {
      this.$query(window).on("scroll", function(e) {
        if (that.$props.index === that.$parent.active) that.scrollHandle(e);
        else {
          that.$query(window).off("scroll");
        }
      });
    }
  },
  watch: {
    "$props.activeTabItem"(v) {
      this.requestData(v);
    },
  },
  methods: {
    loadMoreClickHandle() {
      this.requestData(this.$props.item, true);
    },
    requestData(v, bool) {
      this.listRowsCount = v.listRowsCount;
      this.$store.dispatch("testHomeList", v.api).then((result) => {
        result
          ? (this.listData = result.data) && this.getListData(result.data, bool)
          : typeof v.data !== "undefined" &&
            (this.listData = v.data) &&
            this.getListData(this.listData, bool);
      });
    },
    scrollHandle(e) {
      let that = this;
      let top = e.target.documentElement.scrollTop,
        height = screen.height,
        scrollHeight = e.target.documentElement.scrollHeight;
      that.listPinterestTimeout && clearTimeout(that.listPinterestTimeout);
      that.listPinterestTimeout = setTimeout(() => {
        if (top + height + 60 >= scrollHeight) {
          that.requestData(that.$props.item, true);
        }
      }, 500);
    },
    getListData(data, bool) {
      if (typeof data === "undefined") return;
      if (!this.$Plugins.isArray(data) && typeof data.data !== "undefined")
        data = data.data;
      let that = this,
        listData = { dataLeft: [], dataRight: [], dataCenter: [] },
        names = ["dataLeft", "dataCenter", "dataRight"],
        count = Math.round(data.length / this.listRowsCount);
      if (!this.isLoading) {
        console.log("getListData", data.length);
        this.isLoading = true;
        //console.log(count);
        for (let n = 0; n < data.length; n += count) {
          switch (n) {
            case 0:
              for (let x = n; x < count + n; x++) {
                listData[names[0]].push(data[x]);
              }
              break;
            case count:
              for (let x = n; x < count + n; x++) {
                listData[names[this.listRowsCount === 2 ? 2 : 1]].push(data[x]);
              }
              break;
            default:
              for (let x = n; x < count + n; x++) {
                listData[names[2]].push(data[x]);
              }
              break;
          }
        }
        this.dataLeftListData = bool
          ? this.dataLeftListData.concat(listData.dataLeft)
          : listData.dataLeft;
        this.dataRightListData = bool
          ? this.dataRightListData.concat(listData.dataRight)
          : listData.dataRight;
        if (that.listRowsCount === 3)
          this.dataCenterListData = bool
            ? this.dataCenterListData.concat(listData.dataCenter)
            : listData.dataCenter;
        this.isLoading = false;
      }
    },
  },
};
</script>
<style lang="less">
@base: 23.44rem;
.custom_listpinterest {
  h1 {
    &.custom_listpinterest_header {
      height: (15 / @base);
      font-size: (14 / @base);
      line-height: (14 / @base);
      text-align: left;
      padding: (10 / @base) (10 / @base);
      border-bottom: (1 / @base) solid #efefef;
      display: flex;
      overflow: hidden;
      color: #333;
      font-weight: 400;
      div {
        width: 10%;
        &.custom_listpinterest_header_title {
          width: 25%;
          line-height: (14 / @base);
          overflow: hidden;
          height: (15 / @base);
        }
        &.custom_listpinterest_header_slogan {
          width: 65%;
          font-size: (12 / @base);
          line-height: (14 / @base);
          color: #ccc;
          margin: 0 0 0 (10 / @base);
          font-weight: 400;
        }
      }
    }
  }
  .custom_listpinterest_content {
    display: flex;
    &.nodata {
      span {
        display: block;
        text-align: center;
        font-size: (14 / @base);
        margin: 0 auto;
        line-height: (100 / @base);
      }
    }
    .custom_listpinterest_item {
      width: 50%;
      margin: 0 (2.5 / @base);
      padding: 0;
      &.single {
        width: 100%;
      }
      &:first-child {
        margin: 0 (2.5 / @base) 0 (5 / @base);
      }
      &:last-child {
        margin: 0 (5 / @base) 0 (2.5 / @base);
      }
    }
  }
  .loading {
    padding: (10 / @base) 0 (20 / @base) 0;
  }
  .loadmore {
    font-size: (14 / @base);
    margin: (5 / @base);
    text-align: center;
    /*     background-color: #fff;
    box-shadow: 0 2px 12px rgba(100, 101, 102, 0.12);
 */
    border-radius: (2.5 / @base);
    a {
      padding: (10 / @base);
      color: #999;
      display: block;
    }
  }
}
</style>
